package com.education.wisdom.controller.order;


import com.education.wisdom.pojo.OrderQuestion;
import com.education.wisdom.service.OrderQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 试题表
 *
 * @date 2021-03-04 15:45:19
 */
@RestController
@RequestMapping("/wisdom/orderquestion")
public class OrderQuestionController {

    @Autowired
    private OrderQuestionService orderQuestionService;

    //添加试题
    @PostMapping("/addquestion")
    public void addquestion(HttpServletResponse response, @RequestBody List<OrderQuestion> orderQuestions) throws IOException{

       for(OrderQuestion orderQuestion : orderQuestions){
           Date date = new Date();
           SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String format = dateFormat.format(date);
           orderQuestion.setCreateTime(format);
           int question = orderQuestionService.insertOrderQuestion(orderQuestion);
           writeJSON(question,response, "200", "添加成功");
       }
    }

    //修改试题
    @PostMapping("/updatequestion")
    public void updatequestion(HttpServletResponse response, @RequestBody OrderQuestion orderQuestion) throws IOException{
            int question = orderQuestionService.updateOrderQuestion(orderQuestion);
            writeJSON(question,response,"200","修改成功");
    }

    //查询试题列表  根据试题类型从试题表中查询试题
    @GetMapping("/selectlist")
    public void selectquestionlist(HttpServletResponse response, OrderQuestion orderQuestion) throws IOException{
        List<OrderQuestion> orderQuestions = orderQuestionService.selectOrderQuestionList(orderQuestion);
        writeJSON(orderQuestions,response,"200","查询成功");
    }

   //删除单个试题
    @GetMapping("/deletequestion")
    public void deletequestion(HttpServletResponse response, int id) throws IOException{
        OrderQuestion orderQuestion = orderQuestionService.selectOrderQuestionById(id);
        if(orderQuestion!= null){
            writeJSON(orderQuestionService.deleteOrderQuestionById(id),response,"200","删除成功");
        }else {
            writeJSON(null,response,"201","删除失败");
        }
    }
}
